<template>
	<view class="mine-coupon-use">
		<view class="list-item fill-base mg-big pd-lg radius-34" v-for="(item,index) in list.data" :key="index">
			<view class="flex-between">
				<view class="flex-center flex-column">
					<view class="flex-y-baseline f-caption c-warning">¥<view class="f-lg-title">{{item.discount}}
						</view>
					</view>
					<view class="f-caption c-title">优惠券</view>
				</view>
				<view class="flex-1 ml-lg flex-between">
					<view class="f-title c-title mr-lg">
						<view class="text-bold">{{item.title}}</view>
						<view class="f-icontext mt-sm">{{item.type==0?`满${item.full}元可用`:'无门槛'}}</view>
						<view @tap.stop="this.list.data[index].is_show = !this.list.data[index].is_show"
							class="flex-y-baseline f-icontext mt-sm">查看详情
							<i class="iconfont ml-sm" style="font-size: 24rpx;"
								:class="[{'icon-down':!item.is_show},{'icon-up':item.is_show}]"></i>
						</view>
					</view>
					<view @tap.stop="toUse(index)" class="use-btn flex-center f-caption c-base radius"
						:style="{background:primaryColor}">
						去使用
					</view>
				</view>
			</view>
			<view class="mt-lg pd-lg f-icontext c-title radius-16" style="background: #F9FAF9" v-if="item.is_show">
				<view class="flex-warp">
					<view>卡券数量：</view>
					<view class="flex-1 c-desc">x{{item.num}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用时间：</view>
					<view class="flex-1 c-desc">{{item.start_time}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用范围：</view>
					<view class="flex-1 c-desc">{{item.use_range}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用规则：</view>
					<view class="flex-1 c-desc">
						<text decode="emsp" style="word-break:break-all;">{{item.rule}}</text>
					</view>
				</view>
				<view class="flex-warp mt-md">
					<view>优惠详情：</view>
					<view class="flex-1 c-desc">
						<text decode="emsp" style="word-break:break-all;">{{item.text}}</text>
					</view>
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor type="COUPON" v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<view class="space-max-footer"></view>

		<fix-bottom-button @confirm="noUse" :text="[{type:'confirm',text:'不使用卡券'}]">
		</fix-bottom-button>
	</view>
</template>

<script>
	import {
		mapState,
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				useType: {
					is_land: '土地租赁',
					is_claim: '认养服务',
					is_shop: '商城',
				},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			userInfo: state => state.user.userInfo,
			orderInfo: state => state.order.orderInfo,
		}),
		onLoad(options) {
			this.options = options
			this.$util.showLoading()
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return
			this.param.page = this.param.page + 1
			this.loading = true
			this.getList()
		},
		methods: {
			initIndex() {
				this.getList()
			},
			initRefresh() {
				this.param.page = 1
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					useType
				} = this
				let {
					type = 1
				} = this.options
				let land_param = type == 1 ? this.$util.pick(this.orderInfo, ['land_id', 'spe_id', 'cycle',
					'massif_id',
					'seed_data'
				]) : {}
				let param = Object.assign({}, this.param, this.options, land_param)
				let method = {
					1: 'land',
					2: 'claim',
					3: 'shop'
				}
				delete param.type
				let newList = await this.$api[method[type]].canUseCouponList(param)
				newList.data.map(item => {
					item.is_show = false
					let use_range = ''
					for (let key in useType) {
						if (item[key]) {
							use_range += `${useType[key]}、`
						}
					}
					item.use_range = use_range.substring(0, use_range.length - 1)
				})

				if (this.param.page == 1) {
					this.list = newList
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList
				}
				this.loading = false
				this.$util.hideAll()
			},
			toUse(index) {
				if (this.lockTap) return
				this.lockTap = true
				try {
					let {
						id
					} = this.list.data[index]
					this.$util.getPage(-1).orderInfo.coupon_id = id
					this.$util.back()
					setTimeout(() => {
						this.$util.goUrl({
							url: 1,
							openType: 'navigateBack'
						})
					}, 500)
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
					}, 2000)
				}
			},
			noUse() {
				this.$util.getPage(-1).orderInfo.coupon_id = 0
				this.$util.back()
				setTimeout(() => {
					this.$util.goUrl({
						url: 1,
						openType: 'navigateBack'
					})
				}, 500)
			}
		}
	}
</script>


<style lang="scss">
	.mine-coupon-use {
		.list-item {
			overflow: hidden;

			.item-title {
				top: 0;
				left: 0;
				height: 50rpx;
				background: #FFE2E2;
				border-radius: 15rpx 0 15rpx 0;
			}

			.use-btn {
				min-width: 120rpx;
				height: 58rpx;
			}
		}
	}
</style>
